<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :visible.sync="dialog" title="角色选择列表" width="800px">
    <!--表格渲染-->
    <div v-if="mult_select">
      <DataGridTable
        v-loading="loading"
        :sup_this="sup_this"
        :data="data.rtnList"
        :multi-select="false"
        :show-row-number="true"
        :show-page="false"
        height="400px"
        table-ref="roleTable">
        <el-table-column prop="id" width="90px">
          <template slot="header" slot-scope="scope">
            <el-checkbox v-model="checkAll" @change="handleCheckAllChange" />
          </template>
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="roleCheckSelectChange(scope.row)">{{ selectedRows[scope.row.id] ? '取消' : '选择' }}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="roleName" label="名称">
          <template slot="header" slot-scope="scope">
            <el-input v-model="roleName" clearable placeholder="输入回车过滤" style="width: 100%;" class="filter-item" @keyup.enter.native="filterQuery"/>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="rmk" label="描述"/>
      </DataGridTable>
    </div>
    <div v-else>
      <DataGridTable
        v-loading="loading"
        :sup_this="sup_this"
        :data="data.rtnList"
        :multi-select="mult_select"
        :show-row-number="true"
        :show-page="false"
        height="400px"
        table-ref="roleTable"
        @current-change="handleCurrentChange">
        <el-table-column prop="roleName" label="名称">
          <template slot="header" slot-scope="scope">
            <el-input v-model="roleName" clearable placeholder="输入回车过滤" style="width: 100%;" class="filter-item" @keyup.enter.native="filterQuery"/>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="rmk" label="描述"/>
      </DataGridTable>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import initData from '@/mixins/initData'
import { findRoleByCpyCodeUrl } from '@/api/system/role'
import { parseTime } from '@/utils/index'
import { getUserInfo } from '@/utils/auth'

export default {
  mixins: [initData],
  props: {
    mult_select: {
      default: true,
      requried: false,
      type: Boolean
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      selectedRows: [],
      selectedRow: {},
      sup_this: this,
      allList: [],
      checkAll: false,
      roleName: ''
    }
  },
  created() {
    this.url = findRoleByCpyCodeUrl()
    this.pageCount = false
  },
  methods: {
    parseTime,
    async beforeInit() {
      this.selectedRows = []
      this.checkAll = false
      this.selectedRow = {}
      this.paramBean.sysOwnerCpy = getUserInfo().cpyCode
      return true
    },
    afterInit(res) {
      this.allList = res.data.rtnList
    },
    filterQuery() {
      const roleName = this.roleName
      if (roleName) {
        this.data.rtnList = this.allList.filter(row => {
          return row.roleName.includes(roleName)
        })
      } else {
        this.data.rtnList = this.allList
      }
    },
    handleCurrentChange(val) {
      this.selectedRow = val
    },
    doSubmit() {
      this.loading = true
      if (this.mult_select) {
        const _selectRows = this.selectedRows
        const tmpKeys = Object.keys(_selectRows)
        const tmpRows = []
        for (const tmpKey of tmpKeys) {
          tmpRows.push(_selectRows[tmpKey])
        }
        this.$emit('roleSelectCallback', tmpRows, this)
      } else {
        this.$emit('roleSelectCallback', this.selectedRow, this)
      }
    },
    cancel() {
      this.dialog = false
      this.allList = []
    },
    handleCheckAllChange(isChecked) {
      const tmpSelectRows = this.selectedRows
      if (isChecked) {
        this.data.rtnList.forEach(row => {
          tmpSelectRows[row.id] = row
        })
      } else {
        // 取消
        this.data.rtnList.forEach(row => {
          delete tmpSelectRows[row.id]
        })
      }
      this.selectedRows.splice(this.selectedRows.length)
    },
    roleCheckSelectChange(roleRow) {
      if (!this.selectedRows[roleRow.id]) {
        // 选择
        this.selectedRows[roleRow.id] = roleRow
      } else {
        delete this.selectedRows[roleRow.id]
      }
      this.selectedRows.splice(this.selectedRows.length)
    }
  }
}
</script>
